package gruppe7.model.db;


import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBAccess {

    private static String CONTEXT = "java:comp/env";
    private static String CONTEXT_KEY = "jdbc/mysql";
    private static DBAccess instance = null;

    
    private DataSource dataSource;
    
    
    public static DBAccess getInstance() throws Exception {
    	if (instance == null) {
            instance = new DBAccess();
        }

        return instance;
    }

    private DBAccess() throws Exception {
    	try {
    	
    		Context initial = new InitialContext();
    		Context ctx = (Context) initial.lookup(CONTEXT);

    		dataSource = (DataSource) ctx.lookup(CONTEXT_KEY);
    	} catch (Exception e) {
    		System.err.println(e);
    		throw e;
    	}
    }

    public Connection getConnection() throws Exception {
    	
    	try {
    		return dataSource.getConnection();
    	} catch (Exception e) {
    		System.err.println(e);
    		throw e;
    	}    	
    }
}
